package com.ztz.mynewdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.GetCallback;
import com.bumptech.glide.Glide;
import com.ztz.mynewdemo.R;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DynamicDetailActivity extends BaseActivity {
    public static final String OBJECT_ID = "objectId";
    @BindView(R.id.content_detail)
    TextView contentDetail;
    @BindView(R.id.image_detail)
    ImageView imageDetail;
    @BindView(R.id.name_detail)
    TextView nameDetail;
    @BindView(R.id.dy_time)
    TextView dyTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_dynamic_detail);
        ButterKnife.bind(this);
        setToolbar("动态详情");
        final SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM:dd HH:mm:ss");
        String getObjectId = getIntent().getStringExtra(OBJECT_ID);
        AVObject avObject = AVObject.createWithoutData("Product", getObjectId);
        avObject.fetchInBackground("owner", new GetCallback<AVObject>() {
            @Override
            public void done(final AVObject avObject, AVException e) {
                String date=dateFormat.format(avObject.getUpdatedAt());
                dyTime.setText(date);
                nameDetail.setText(avObject.getAVUser("owner") == null ? "" : avObject.getAVUser("owner").getUsername());
                contentDetail.setText(avObject.getString("content"));
                Glide.with(DynamicDetailActivity.this).load(avObject.getAVFile("image") == null ? "www" : avObject.getAVFile("image").getUrl()).into(imageDetail);
                imageDetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(DynamicDetailActivity.this, PhotoDynamicActivity.class);
                        intent.putExtra(PhotoDynamicActivity.IMAGE_URL, avObject.getAVFile("image").getUrl());
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        AVAnalytics.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AVAnalytics.onResume(this);
    }

}
